﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Perceptron.Engine
{
    internal class LockedBitmap : IDisposable
    {
        public const PixelFormat CommonPixelFormat = PixelFormat.Format24bppRgb;

        public const int BytesPerPixel = 3;

        public LockedBitmap(Bitmap bitmap, ImageLockMode? imageLockMode = null)
        {
            imageLockMode = imageLockMode ?? ImageLockMode.UserInputBuffer;

            Bitmap = bitmap;
            BitmapData = bitmap.LockBits(bitmap.GetFullSizeRectangle(), imageLockMode.Value, LockedBitmap.CommonPixelFormat);
        }

        public BitmapData BitmapData { get; private set; }

        public Bitmap Bitmap { get; set; }

        private bool _isDisposed = false;

        //~LockedBitmap()
        //{
        //    ((IDisposable)this).Dispose();
        //}

        void IDisposable.Dispose()
        {
            if (_isDisposed) return;
            _isDisposed = true;

            Bitmap.UnlockBits(BitmapData);
        }
    }
}
